/**
 * 
 */
package com.lanswon.qzsmk.listener;

import java.util.List;

import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * webservice认证
 * @author sun
 *
 */
public class SOAPAuthIntercepter extends AbstractPhaseInterceptor<SoapMessage> {


	org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());
	private static final String NAME="admin";
	private static final String PW="admin";
	
	public SOAPAuthIntercepter() {
		super(Phase.PRE_INVOKE);
		
	}

	@Override
	public void handleMessage(SoapMessage meg) throws Fault{
		List<Header> headers=meg.getHeaders();
		if(headers==null||headers.size()==0){
			throw new Fault(new Throwable("message have no request header"));
		}else{
			for(Header header:headers){
				Element element=(Element) header.getObject();
				NodeList nodesName=element.getElementsByTagName("name");
				NodeList nodesPW=element.getElementsByTagName("passWord");
				if(nodesName!=null&&nodesPW!=null){
					String name=nodesName.item(0).getTextContent();
					String passWord=nodesPW.item(0).getTextContent();
					if(!PW.equals(passWord)||!NAME.equals(name)){
						throw new Fault(new Throwable("name or password exception"));
					}
				}
				
			}
		}
		
	}
    
}
